package tempo.timing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class StartActivity extends Activity
{
	private static class EfficientAdapter extends BaseAdapter
	{
		private LayoutInflater mInflater;
		private ArrayList<Rider> mRiders;

		public EfficientAdapter(Context context, ArrayList<Rider> riders)
		{
			mInflater = LayoutInflater.from(context);
			mRiders = riders;
		}

		public int getCount()
		{
			return mRiders.size();
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder holder;
			if (convertView == null)
			{
				convertView = mInflater.inflate(R.layout.listview, null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView.findViewById(R.id.TextView01);
				holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
				convertView.setTag(holder);
				convertView.setBackgroundColor((position & 1) == 1 ? Color.WHITE : Color.LTGRAY);
				if (mRiders.get(position).getStartTime() > 0)
					convertView.setBackgroundColor(Color.GREEN);

			} else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.text.setText(mRiders.get(position).getStartNumber());
			holder.text2.setText(mRiders.get(position).getRiderName());
			return convertView;
		}

		static class ViewHolder
		{
			TextView text;
			TextView text2;
		}
	}

	private Button start;
	private ListView ridersListView;
	private ArrayList<Rider> mRiders;
	private TextView txtStartNumber;
	private TextView txtRiderName;
	private int selectedRider = -1;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start);

		txtStartNumber = (TextView) findViewById(R.id.txtStartNumber);
		txtRiderName = (TextView) findViewById(R.id.txtRiderName);

		start = (Button) findViewById(R.id.startButton);
		start.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				if (selectedRider > -1 && selectedRider < mRiders.size())
				{
					Rider rider = mRiders.get(selectedRider);
					rider.setStartTime(System.currentTimeMillis());
					String message = mRiders.get(selectedRider).getRiderName() + " has started";
					ToastHelper.DisplayToast(getBaseContext(), message);
					Race.getCurrent().SaveJson();
					refreshList();
				}
			}
		});

		ridersListView = (ListView) findViewById(R.id.ridersListView);
		ColorDrawable divcolor = new ColorDrawable(Color.DKGRAY);
		ridersListView.setDivider(divcolor);
		ridersListView.setDividerHeight(2);
		ridersListView.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				selectedRider = arg2;
				txtStartNumber.setText(mRiders.get(selectedRider).getStartNumber());
				txtRiderName.setText(mRiders.get(selectedRider).getRiderName());
				if(mRiders.get(selectedRider).getStartTime()> 0)
					ToastHelper.DisplayToast(getBaseContext(), "Rider allready started");
			}
		});
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		refreshList();
	}
	private class StartedAtBottomComparator implements Comparator<Rider>
	{
		public int compare(Rider object1, Rider object2)
		{
			if(object1.getStartTime() < 0 && object2.getStartTime() < 0)
				return 0;
			if(object1.getStartTime() < 0 )
				return -1;
			if(object2.getStartTime() < 0 )
				return 1;	
			return object1.getStartNumber().compareTo(object2.getStartNumber());	
		}
	
	}

	@SuppressWarnings("unchecked")
	private void refreshList()
	{
		mRiders = (ArrayList<Rider>) Race.getCurrent().GetRiders().clone();
		Collections.sort(mRiders, new StartedAtBottomComparator());
		ridersListView.setAdapter(new EfficientAdapter(this, mRiders));
	}
}
